<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title>Title</title>

    <script src="./js/vue.min.js"></script>
    <script src="./js/axios.js"></script>
</head>
<body>
    <div id="app">
        <input type="button" value="查询" @click="findUserList">
        <hr/>
        name:<input type="text" v-model="user.name"><br/>
        age:<input type="number" v-model="user.age"><br/>
        <input type="button" value="新增" @click="addUser">
        <hr/>
        <input type="text" v-model="user.name"><input type="button" value="修改" @click="updateUser"></input>
        <hr/>
        <input type="button" value="删除" @click="deleteUser">
        <hr/>
        <ul>
            <li v-for="t in userList">{{t}}</li>
        </ul>
    </div>
</body>
</html>
<script>
    var vm=new Vue(
        {
            el: "#app",
            data:{
                userList:[],
                user:{
                    name:"",
                    age:0
                }
            },
            methods:{
                findUserList(){
                    var _this=this
                    // axios.get("http://127.0.0.1:8080/text").then(function(data){
                    //     console.log(data);
                    //     _this.textList=data.data;
                    // })
                    //更多参考https://www.npmjs.com/package/axios
                    axios.get("http://127.0.0.1:8080/user").then((data)=>{
                        console.log(data);
                        _this.userList=data.data;
                    })
                },
                addUser(){
                    var _this=this;
                    console.log(_this.user);
                    axios({
                        method:"post",
                        url:"http://127.0.0.1:8080/user",
                        data:{
                            name: _this.user.name,
                            age:_this.user.age
                        }
                    }).then((data)=>{
                        console.log(data);
                        _this.findUserList()
                    });

                },
                updateUser(){
                    var _this=this;
                    axios({
                        method:"put",
                        url:"http://127.0.0.1:8080/user",
                        data:{
                            name:_this.user.name,
                            age:100
                        }
                    }).then((data)=>{
                        console.log(data);
                        _this.findUserList();
                    });
                },
                deleteUser(){
                    var _this=this
                    axios.delete("http://127.0.0.1:8080/user",{params: {userName:"tzx"}}).then((data)=>{
                        console.log(data);
                        _this.findUserList();
                    })
                }
            }
        }
    );
</script>